package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    /**
     * 敏感词分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult queryPage(SensitiveDto dto) {
        // 参数校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 分页参数校正
        dto.checkParam();

        // 添加分页查询条件
        IPage<AdSensitive> iPage = new Page<>(dto.getPage(), dto.getSize());
        // 添加条件查询
        LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper<>();
        // 模糊匹配
        if (StringUtils.isNotEmpty(dto.getName())){
            queryWrapper.like(AdSensitive::getSensitives, dto.getName());
        }
        // 根据创建时间倒序
        queryWrapper.orderByDesc(AdSensitive::getCreatedTime);

        // 分页查询
        iPage = this.page(iPage, queryWrapper);

        // 结果封装返回
        PageResponseResult pageResult = new PageResponseResult();
        pageResult.setData(iPage.getRecords());
        pageResult.setCurrentPage((int) iPage.getCurrent());
        pageResult.setSize((int) iPage.getSize());
        pageResult.setTotal((int) iPage.getTotal());

        return pageResult;
    }

    @Override
    public ResponseResult add(AdSensitive sensitive) {
        // 参数校验
        if (sensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 判断添加的敏感词是否已经存在
        // 查询所有的敏感词
        List<AdSensitive> adSensitiveList = this.list();
        for (AdSensitive adSensitive : adSensitiveList) {
            if (adSensitive.getSensitives().equals(sensitive.getSensitives())){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
        }

        // 初始化属性
        sensitive.setCreatedTime(new Date());
        // 保存
        this.save(sensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
        // 参数校验
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }



}
